
#include "Player.h"
#include "MouseSource.h"
#include "Evironment.h"
#include "SimpleMap.h"
namespace Core
{
	using namespace Devices;
	namespace GameLogic
	{
		Player::Player()
			:currentReceiver(0),IsMouseRight(false)
		{
			SimpleMap* e=&TEvironment<SimpleMap>::getInstance()->GetScene().GetMap();
			MouseSource::getInstance()->SetMouseBoundary(0,e->GetMapSize().height-1,0,e->GetMapSize().width-1);
			MouseSource::getInstance()->add(this);
		}
		using Core::Position;
		void Player::OnMouseEvent (SourceType& mouse)
		{
			SimpleMap* e=&TEvironment<SimpleMap>::getInstance()->GetScene().GetMap();
			int x=mouse.x;
			int y=mouse.y;
			SimpleMap::MapInfo info=e->GetInfo(Position(x,y));
			if(mouse.left)
			{
				if(!info.elements.empty())
				{
					this->currentReceiver=*info.elements.begin();
				}
				else
					this->currentReceiver=0;
			}
			else if(mouse.right && !IsMouseRight)
			{
				if(currentReceiver)
				{
					ParamList* p=new ParamList();

					Core::Position t=e->PositionOnGrid(Position(x,y));
					if(t==currentReceiver->GetPosition())
						return;
					p->Add("des",Param(Core::POSITION,new Core::Position(t.x,t.y)));
					currentReceiver->ReceiveCommand(Core::Command("move",p));
					
				}
				IsMouseRight=true;
			}
			else if(!mouse.right && IsMouseRight)
				IsMouseRight=false;
				

		}
	}
}